package cn.itsource.basic.jobtask;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.mapper.OrderAdoptMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* 定时任务执行类
* @author wangcp
* @date 2021/05/10 10:42
**/
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;


    @Async("taskExecutor")
    @Override
    @Transactional
    public void executeRemindMessage() throws InterruptedException {
        //写超时业务的....

        //1.查询订单   状态是待支付state = 0   时间最近的20分钟(当前时间往前推20分钟)  1分钟  16分钟

        List<OrderAdopt> orders = orderAdoptMapper.findOrders();
        //2.判断时间和当前时间隔了15分钟以上,才会进行关闭
        orders.stream().filter(orderAdopt -> DateUtil.between(new Date(), orderAdopt.getLastConfirmTime(), DateUnit.SECOND) > 900)
            .forEach(orderAdopt -> {
                orderAdopt.setState(2);//取消
                orderAdoptMapper.updateByPrimaryKeySelective(orderAdopt);
            });
    }

    public static void main(String[] args) {
        Date date = DateUtils.addMinutes(new Date(), -20);
        System.out.println(date);

        long between = DateUtil.between( new Date(),date, DateUnit.SECOND);
        System.out.println(between);
    }
}